<template>
	<view class="content">
		<view class="back_icon" @click="backUp">
			<image
				src="/static/icons/back.png"
				mode="scaleToFill"
			/>
			<text>返回</text>
		</view>
		<view class="head">
			<image src="/static/login26_bg.png"></image>
		</view>
		<view class="title">
			<text class="name">Hi,{{greeting == null ? '您好':greeting}}!</text>
			<text class="sub-name">欢迎注册{{$sysName}}~</text>
		</view>
		<view class="form-box">
			<view class="row-input">
				<image src="/static/login26_account.png"></image>
				<input v-model="form.username" placeholder="请输入账号"/>
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input v-model="form.password" placeholder="输入密码" password />
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input v-model="form.passwordCheck" placeholder="输入确认密码" password />
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input v-model="form.email" placeholder="输入邮箱" type="email"/>
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input type="text" v-model="form.code" placeholder="输入验证码" maxlength="6"/>
				<!-- <button type="primary" :disabled="buttonFlag" :loading="buttonFlag" @click="sendEmailCode">{{buttonText}}</button> -->
				<button type="primary" @click="sendEmailCode" :disabled="countdown > 0">
					{{ countdown > 0 ? `${countdown}秒后重新发送` : '获取验证码' }}
				</button>
			</view>
			<!-- <view class="menu-link">
				<text @click="register">注册</text>
				<text @click="resetpwd">忘记密码?</text>
			</view> -->
			<view class="login-btn" @click="register">注册</view>
			<view class="tip-link">
				注册即代表你已同意<text>用户协议</text>和<text>隐私政策</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from "@/common/apiUtil.js"
	export default {
		data() {
			return {
				form:{
					username: null,
					password: null,
					passwordCheck: null,
					email: null,
					code: null
				},
				buttonFlag:false,
				buttonText:"发送验证码",
				countdown: 0,
      			intervalId: null
			}
		},
		onShow() {
			let token = uni.getStorageSync("token");
			if(token){
				uni.reLaunch({
					url:"/pages/index/index"
				})
				return;
			}
			
			this.setGreeting();
		},
		methods:{
			getKey(){
				let timestamp = Date.now();
				this.form.codeKey = timestamp;
				return timestamp;
			},
			setGreeting() {
			  const now = new Date();
			  const hours = now.getHours();
		
			  if (hours < 5) {
				this.greeting = '凌晨好';
			  } else if (hours < 12) {
				this.greeting = '上午好';
			  } else if (hours < 18) {
				this.greeting = '下午好';
			  } else {
				this.greeting = '晚上好';
			  }
			},
			backUp(){
				uni.redirectTo({ url: '/pages/login/login' })
			},
			sendEmailCode(){
				let emailText = this.form.email;
				if(!emailText){
					uni.showToast({
						title: '邮箱不能为空!',
						icon:"none"
					});
					return;
				}
				this.$get(api.GET_EMAIL_RANDOM_CODE,{email:emailText}).then(res=>{
					console.log(res)
					if(res.success){
						uni.showToast({
							title: res.message,
							icon:"none"
						});
						if (this.countdown === 0) {
							this.countdown = 60;
							this.intervalId = setInterval(() => {
								this.countdown--;
								if (this.countdown === 0) {
									clearInterval(this.intervalId);
								}
							}, 1000);
						}
					}
				})
			},
			register(){
				this.$post(api.USER_REGISTER,this.form).then(res=>{
					console.log(res)
					uni.showToast({
						title: res.message,
						icon:"none"
					});
					if(res.success){
						uni.setStorageSync("token", "Bearer " + res.result);
						// 登录成功后跳转到首页
						setTimeout(function() {
						    uni.reLaunch({
						        url: "/pages/index/index"
						    });
						}, 1000);
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #e9edf6;
	}
	.back_icon{
		position: fixed;
		top: 80rpx;
		left: 30rpx;
		image{
			width: 50rpx;
			height: 50rpx;
			vertical-align: middle;
		}
	}

	.head {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		width: 100%;
		height: 30vh;
		z-index: -1;
		image {
			width: 100%;
			height: 100%;
		}
	}

	.title {
		padding: 200rpx 0rpx 30rpx 80rpx;
		display: flex;
		justify-content: center;
		align-items: flex-start;
		flex-direction: column;

		.name {
			font-size: 70rpx;
			line-height: 100rpx;
			font-weight: bold;
		}

		.sub-name {
			line-height: 80rpx;
			font-size: 45rpx;
			letter-spacing: 8rpx;
			font-weight: bold;
		}
	}

	.form-box {
		padding: 0rpx 80rpx;

		.row-input {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 100rpx;
			background-color: #FFFFFF;
			border-radius: 12rpx;
			margin: 30rpx 0rpx;

			image {
				margin: 0 25rpx;
				flex-shrink: 0;
				width: 31rpx;
				height: 35rpx;
			}

			input {
				font-size: 30rpx;
				flex: 1;
			}
			
			button{
				// width: 180rpx;
				height: 70rpx;
				line-height: 70rpx;
				margin-right: 10rpx;
				font-size: 23rpx;
				background-color: #819682;
			}
		}

		.menu-link {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 26rpx;
		}

		.login-btn {
			margin-top: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 90rpx;
			border-radius: 12rpx;
			background-color: #819682;
			color: #f4f4f4;
			font-size: 30rpx;
			letter-spacing: 5rpx;
			box-shadow: 0rpx 5rpx 20rpx #94ac95;
		}

		.tip-link {
			display: flex;
			justify-content: center;
			letter-spacing: 3rpx;
			line-height: 150rpx;
			font-size: 26rpx;

			text {
				color: #57a2ee;
			}
		}

	}
</style>
